<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品分类" prop="productType">
        <el-select v-model="queryParams.productType" placeholder="请选择产品分类">
                <el-option 
                v-for="productType in productTypeOptions"
                :key="productType.productTypeId"
                :label="productType.productTypeName"
                :value="productType.productTypeId"/>
              </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['product:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="productId" />
      <el-table-column label="公司简称" align="center" prop="companySimpleName" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品编号" align="center" prop="productNum" />
      <!-- <el-table-column label="产品介绍" align="center" prop="productInfo" /> -->
      <!-- <el-table-column label="产品功能" align="center" prop="productFunction" /> -->
      <el-table-column label="演示地址" align="center" prop="demonstration" />
      <el-table-column label="背景颜色" align="center" prop="backgroundColor" />
      <!-- <el-table-column label="产品白皮书" align="center" prop="whitePaper" /> -->
      <el-table-column label="测试账号" align="center" prop="account" />
      <el-table-column label="产品分类" align="center" prop="productType"  :formatter="productTypeFormat" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司简称" prop="companySimpleName">
              <el-input v-model="form.companySimpleName" placeholder="请输入公司简称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" placeholder="请输入产品名称" />
            </el-form-item>
          </el-col>
        </el-row>
       
       <el-row>
          <el-col :span="12">
            <el-form-item label="产品编号" prop="productNum">
              <el-input v-model="form.productNum" placeholder="请输入产品编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品分类" prop="productType">
              <el-select v-model="form.productType" placeholder="请选择产品分类">
                <el-option 
                v-for="productType in productTypeOptions"
                :key="productType.productTypeId"
                :label="productType.productTypeName"
                :value="productType.productTypeId"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        


        <el-form-item label="产品介绍" prop="productInfo">
          <el-input v-model="form.productInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="产品功能" prop="productFunction">
          <el-input v-model="form.productFunction" type="textarea" placeholder="功能1,功能2," />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="演示地址" prop="demonstration">
              <el-input v-model="form.demonstration" placeholder="请输入演示地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
               <el-form-item label="背景颜色" >
                  <el-col :span="13">
                    <el-form-item prop="backgroundColor">
                      <el-input v-model="form.backgroundColor" placeholder="请输入背景颜色" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item >
                      <el-color-picker v-model="form.backgroundColor"></el-color-picker>
                    </el-form-item>
                  </el-col>
                </el-form-item>
          </el-col>
        </el-row>
        
       
        <el-form-item label="产品白皮书">
          <fileUpload v-model="form.whitePaper"  :fileSize="textFileSize" :fileType="textFileType"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="测试账号" prop="account">
              <el-input v-model="form.account" placeholder="请输入测试账号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="排序" prop="orderNum">
                <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
              </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct, exportProduct } from "@/api/product/product";
import FileUpload from '@/components/FileUpload';
import { listAllType } from "@/api/product/type";

export default {
  name: "Product",
  components: {
    FileUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品管理表格数据
      productList: [],
      // 产品分类
      productTypeOptions:[],
      // 0显示1隐藏字典
      statusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      textFileType: ["doc", "xls", "ppt", "txt", "pdf", "gz", "zip", "rar", "docx", "ppt", "pptx"],
      textFileSize: 500,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getProductTypeList();
     this.getDicts("sys_show_hide").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询产品管理列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询所有产品分类
    getProductTypeList() {
      var queryParams = {
         status: 0
      }
      listAllType(this.queryParams).then(response => {
        this.productTypeOptions = response.data;
      });
    },
    productThemeChange(val) {
      this.form.backgroundColor = val;
    },
    //分类格式化
     productTypeFormat(row, column){
      return this.selectCommonLabel(this.productTypeOptions, row.productType,'productTypeName','productTypeId');
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        companySimpleName: null,
        productName: null,
        productNum: null,
        productInfo: null,
        productFunction: null,
        demonstration: null,
        backgroundColor: null,
        whitePaper: null,
        account: null,
        orderNum: null,
        remark: null,
        productType: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids
      getProduct(productId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.productId != null) {
            updateProduct(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$confirm('是否确认删除产品管理编号为"' + productIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProduct(productIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportProduct(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
